<template>
  <el-dialog :visible.sync="visible" width="70%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="130px" class="dialog-form" :disabled="type == 'view'">

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="取水户编码" prop="ecNum">
            <el-autocomplete
              style="width: 100%;"
              v-model="form.ecNum"
              :fetch-suggestions="queryEcNum"
              placeholder="请输入取水户编码"
              @select="handleEcNumSelect"
              clearable
            >
             <template slot-scope="{ item }">
              <div class="name">{{ item.ecNum }}</div>
              <span class="addr">{{ item.engName }}</span>
            </template>
          </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="取水户名称" prop="ecName">
            <el-input v-model="form.ecNumName" placeholder="请输入取水户名称"></el-input>
          </el-form-item>
        </el-col>
        </el-row>

        <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="取水口编码" prop="ecNumSub">
            <el-autocomplete
              v-model="form.ecNumSub"
              style="width: 100%;"
              :fetch-suggestions="queryEcNumSub"
              placeholder="请输入取水口编码"
              @select="handleEcNumSubSelect"
              clearable
            >
             <template slot-scope="{ item }">
              <div class="name">{{ item.ecNumSub }}</div>
              <span class="addr">{{ item.name }}</span>
            </template>
          </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="取水口名称" prop="ecNameSub">
            <el-input v-model="form.ecNumSubName" placeholder="请输入取水口名称"></el-input>
          </el-form-item>
        </el-col>
        </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="行政分区" prop="areaCode">
            <el-cascader ref="areaCodeRef" v-model="form.areaCode" :options="areaTree"  :props="{expandTrigger: 'hover',label: 'name', value: 'areaCode', checkStrictly: true}" placeholder="请选择" clearable @change="handleAreaCodeChange"></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="流域分区" prop="basinCode">
            <el-cascader ref = "basinCodeRef" v-model="form.basinCode" :options="basinOptions" :props="{expandTrigger: 'hover',label: 'name', value: 'basinCode', checkStrictly: true}" placeholder="请选择" clearable @change="handleBasinCodeChange"></el-cascader>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="统计水量" prop="amount">
            <el-input v-model="form.amount" placeholder="请输入统计水量" type="number" min="0"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="统计用途 " prop="purpose">
<!--            <el-input v-model="form.purpose" placeholder="请输入统计用途" ></el-input>-->
            <el-select
              size="small"
              v-model="form.purpose"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in purposeOptions"
                :key="item.name"
                :label="item.name"
                :value="item.name"
              >
              </el-option>
            </el-select>

          </el-form-item>
        </el-col>

      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="蓄水水量" prop="retainWater">
            <el-input v-model="form.retainWater" placeholder="请输入蓄水水量" type="number" min="0"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="引水水量 " prop="diversionWater">
            <el-input v-model="form.diversionWater" placeholder="请输入引水水量" type="number" min="0"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="提水水量" prop="carryWater">
            <el-input v-model="form.carryWater" placeholder="请输入提水水量"></el-input>
          </el-form-item>
        </el-col>

      </el-row>

    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {
        dictName: '',
        basinUnit: [],
        monitoringTime: ''
      },
      purposeOptions:[
        {
          "name":"供水非重点"
        },
        {
          "name":"重点供水"
        },
        {
          "name":"非重点服务业"
        },
        {
          "name":"非重点工业"
        },
        {
          "name":"重点服务业"
        },
        {
          "name":"小型灌区"
        },
        {
          "name":"重点工业"
        },
        {
          "name":"大型灌区"
        },
      ],

      rules: {
        areaCode: [{ required: true, message: '请选择行政分区', trigger: 'blur' }],
        basinCode: [{ required: true, message: '请选择流域分区', trigger: 'blur' }],
        ecNum: [{ required: true, message: '请输入取水口编码', trigger: 'blur' }],
        ecNum: [{ required: true, message: '请输入取水口编码', trigger: 'blur' }],
        ecNumSub: [{ required: true, message: '请输入取水户编码', trigger: 'blur' }],
        carryWater: [{ required: true, message: '请输入提水水量', trigger: 'blur' }],
        longitude: [{ required: true, message: '请输入经度', trigger: 'blur' }],
        latitude: [{ required: true, message: '请输入纬度', trigger: 'blur' }],
        purpose: [{ required: true, message: '请输入统计用途', trigger: 'blur' }],
      },
      type: ''
    };
  },
  computed: {
    ...mapState(["areaTree", "basinOptions"]),
  },
  methods: {
    showModal(type, row) {
      this.visible = true
      this.type = type
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "dictName": "",
          "remark": "",
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
    // 详情
    getInfoInit(id) {
      this.$api.getStatisticsInfo({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (Array.isArray(this.form.areaCode)) {
              this.form.areaCode = parseInt(this.form.areaCode.slice(-1)[0])
            }
            if (Array.isArray(this.form.basinCode)) {
              this.form.basinCode = parseInt(this.form.basinCode.slice(-1)[0])
            }
          if (this.type == 'add') {
            this.$api.addStatistics(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateStatistics(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
    async checkDuplicate() {
      const ecNumSub = this.form.ecNumSub;
      if (!ecNumSub) return;
      // 假设你的后台接口是这样
      const response = await this.$api.checkWaterIntake({ ecNumSub: ecNumSub });
      if (!response.data) {  //重复时
        this.form.ecNumSub = ''
      }
    },
     //查询取水户编码下拉框
     async queryEcNum(queryString, cb) {
      const response = await this.$api.getEcNums({ keywords: queryString });
      if(response.code == 200) {
        cb(response.data)
      }
    },
    async handleEcNumSelect(item) {
      this.form.ecNum = item.ecNum;
      this.form.ecNumName = item.engName;
    },

    //查询取水口编码下拉框
    async queryEcNumSub(queryString, cb) {
      const response = await this.$api.getEcNumSubs({ keywords: queryString });
      console.log('response: ', response);
      if(response.code == 200) {
        cb(response.data)
      }
    },
    async handleEcNumSubSelect(item) {
      this.form.ecNumSub = item.ecNumSub;
      this.form.ecNumSubName = item.name;
    },
    handleAreaCodeChange(val){
            this.$refs.areaCodeRef.dropDownVisible = false;
          },

          handleBasinCodeChange(val){
            this.$refs.basinCodeRef.dropDownVisible = false;
          },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
